package com.hankol.winecellar


import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.baselibrary.view.VipHeardImgView
import com.hankol.winecellar.bean.WineryManagerBean
import com.hankol.winecellar.databinding.ActivityOnlineWinecellarManagerBinding
import com.hankol.winecellar.utils.FlexBoxUtils
import com.hankol.winecellar.viewmodel.WineCellarViewModel
import java.util.*


/**线上酒窖管理
 * @author: merbng
 * @description:
 */
@Route(path = ActivityRouter.WINE_CELLAR.WINCE_ONLINE_WINCELLAR_MANAGER)
class OnlineWineCellarManagerActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: WineCellarViewModel
    private lateinit var binding: ActivityOnlineWinecellarManagerBinding

    override fun getLayoutId(): Int {
        return R.layout.activity_online_winecellar_manager
    }

    override fun init() {
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(WineCellarViewModel::class.java)
        binding.ivBack.setOnClickListener { finish() }
        binding.smartRefresh.setEnableLoadMore(false)
        initListener()
        initObserver()

    }

    override fun onResume() {
        super.onResume()
        requestData()
    }

    private fun requestData() {
        viewModel.reqgetWineryManageData()
    }

    private fun initListener() {
        binding.imgPreview.setOnClickListener(this)
        binding.imgEdit.setOnClickListener(this)
        binding.rlPreview.setOnClickListener(this)
        binding.smartRefresh.setOnRefreshListener {
            requestData()
        }
    }

    private fun initObserver() {
        viewModel.wineryManagerLD.observe(this, Observer {
            binding.smartRefresh.finishRefresh()
            if (it.checkNotNull()) {
                setDataInfo(it)
            }
        })
    }

    var mDataBean: WineryManagerBean? = null
    private fun setDataInfo(bean: WineryManagerBean) {
        bean.let {
            binding.tvAuditIng.visibility = View.GONE
            binding.tvAuditPass.visibility = View.GONE
            binding.rlRefuse.visibility = View.GONE
            mDataBean = it
            //审核状态0待处理 1审核通过 2驳回
            when (it.auditStatus) {
                0 -> {//待处理
                    binding.tvPublishStatus.text = if (it.auditType == 0) "待发布" else "已发布"
                    binding.tvAuditIng.visibility = View.VISIBLE
                    binding.imgEdit.visibility = View.GONE
                }
                1 -> {//审核通过
                    binding.tvPublishStatus.text = "已发布"
                    binding.tvAuditPass.visibility = View.VISIBLE
                    binding.imgEdit.visibility = View.VISIBLE
                }
                2 -> {//驳回
                    binding.tvPublishStatus.text = if (it.auditType == 0) "待发布" else "已发布"
                    binding.rlRefuse.visibility = View.VISIBLE
                    binding.tvAuditRefuse.text = "审核拒绝：${it.rejectContent.checkValue()}"
                    binding.imgEdit.visibility = View.VISIBLE
                }
            }
            binding.rivCover.load(this, it.wineryImg.checkValue())
            binding.tvName.text = it.wineryName.checkValue()
            binding.tvPrice.text = "¥${it.consumptionPrice}/人"
            binding.tvAddress.text = it.detailAddress.checkValue()
            binding.tvDistance.text = CommonUtil.formatDistance(it.distance)

            //标签
            binding.flLabel.removeAllViews()
            var labelList = it.wineryLabelConfigFeature
            if (it.wineryLabelConfigFeature.size > 3) {
                labelList = it.wineryLabelConfigFeature.subList(0, 3)
            }
            for (bean in labelList) {
                FlexBoxUtils.addChildToFlexboxLayout(binding.flLabel, bean)
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rl_preview, R.id.img_preview -> {//预览--查看详情
                mDataBean?.let {
                    if (!DoubleClickHelper.isOnDoubleClick()) {
                        val hashMap = HashMap<String, Any>()
                        hashMap["dealerCode"] = "${mDataBean?.dealerCode}"
                        if (it.auditStatus == 2) {
                            //驳回 显示旧的 传0 默认是0
                            if (it.auditType == 0) {//未创建
                                hashMap["fromType"] = "${WineCellarDetailActivity.FROM_TYPE_ADUIT}"
                            }
                            //驳回  传旧的code
                            hashMap["id"] = "${SpUtils.getInstance().currentWineryInfo.wineryCode.checkValue()}"
                        } else {
                            hashMap["id"] = "${mDataBean?.wineryCode}"
                            hashMap["fromType"] = "${WineCellarDetailActivity.FROM_TYPE_ADUIT}"
                        }
                        ARouterUtils.jump(ActivityRouter.WINE_CELLAR.WINCE_CELLAR_DETAIL, hashMap)
                    }
                }
            }
            R.id.img_edit -> {//编辑
                if (!DoubleClickHelper.isOnDoubleClick()) {
                    val hashMap = HashMap<String, Any>()
                    hashMap["isNewAdd"] = false
                    if (mDataBean?.auditType==0){
                        hashMap["fromType"] = "${EditOnlineWinecellarActivity.FROM_TYPE_ADUIT}"
                    }
                    ARouterUtils.jump(ActivityRouter.WINE_CELLAR.EDIT_ONLINE_WINCELLAR, hashMap)
                }
            }
        }
    }
}